package com.linewow.xhyy.nihaonew.view.input;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.entity.image.ImagesInfo;
import com.linewow.xhyy.nihaonew.entity.tag.CustomTagsInfo;
import com.linewow.xhyy.nihaonew.entity.topic.TopicDetailInfo;
import com.linewow.xhyy.nihaonew.entity.user.Uinfo;
import com.linewow.xhyy.nihaonew.inter.menu.SendInter;

import java.io.File;

/**
 * Created by LXR on 2016/9/1.
 */
public class TopicInputMenu extends LinearLayout {
    private LayoutInflater layoutInflater;
    private FrameLayout primaryFrame,voiceFrame,imgFrame;
    private String TAG="TopicInputMenu";
    private InputVoiceMenu voiceView;
    private View imgView;
    private PrimaryMenu primaryMenu;
    private SendInter sendInter;
    private TopicDetailInfo<ImagesInfo,Uinfo,CustomTagsInfo>topicDetailInfo;


    public void setTopicDetailInfo(TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo> topicDetailInfo) {
        this.topicDetailInfo = topicDetailInfo;
        if(primaryMenu!=null){
            Log.e(TAG,"设置了名字"+topicDetailInfo.uinfo.nickname);
            primaryMenu.setReplyName(topicDetailInfo.uinfo.nickname);
        }

    }

    public TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo> getTopicDetailInfo() {
        return topicDetailInfo;
    }

    public void setSendInter(SendInter sendInter) {
        this.sendInter = sendInter;
    }

    public TopicInputMenu(Context context) {
        super(context);
    }

    public TopicInputMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }
    public TopicInputMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private void init(Context context, AttributeSet attrs) {
        layoutInflater=layoutInflater.from(context);
        layoutInflater.inflate(R.layout.view_input_menu,this);
        primaryFrame= (FrameLayout) findViewById(R.id.input_menu_primary);
        voiceFrame= (FrameLayout) findViewById(R.id.input_menu_voice);
        imgFrame= (FrameLayout) findViewById(R.id.input_menu_pic);
    }





    public void setImgView(View imgView) {
        this.imgView = imgView;
        this.imgView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        imgFrame.addView(this.imgView);
    }

    public void prepareView(final String path){
        primaryMenu= (PrimaryMenu) layoutInflater.inflate(R.layout.menu_primary,null);
        primaryFrame.addView(primaryMenu);

        this.voiceView= (InputVoiceMenu) layoutInflater.inflate(R.layout.menu_add_voice,null);
        voiceView.setPath(path);
        voiceView.setPathSure(primaryMenu);

        this.voiceView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
        voiceFrame.addView(this.voiceView);
        showContainer(0);
        primaryMenu.setListener(new PrimaryMenuBase.PrimaryListener() {
            @Override
            public void voiceClick() {
                showContainer(2);
            }

            @Override
            public void imgClick() {
                showContainer(1);
            }

            @Override
            public void sendMessage(String msg) {
                Log.e(TAG,"接受到的消息2"+msg);
                sendInter.sendMsg(msg);
            }
            @Override
            public void sendPathMessage(boolean flag) {
                sendInter.pathMsg(flag);
            }

            @Override
            public void sendPath(String path) {
                Log.e(TAG,"地址"+path+">>>");
                Log.e(TAG,"是否存在"+new File(path).exists());
                sendInter.pathContent(path);
            }

            @Override
            public void editChange() {
                showContainer(0);
            }
        });
    }

    public void resetView(){
        showContainer(0);
        voiceView.resetView();
        primaryMenu.prepareView();
        this.topicDetailInfo=null;
    }

    public void resetHost(){
        showContainer(0);
        primaryMenu.resetHost();
        this.topicDetailInfo=null;
    }


    public void showContainer(int index){
        switch (index){
            case 0:
                voiceFrame.setVisibility(View.GONE);
                imgFrame.setVisibility(View.GONE);
                break;
            case 1:
                voiceFrame.setVisibility(View.GONE);
                imgFrame.setVisibility(View.VISIBLE);
                break;
            case 2:
                imgFrame.setVisibility(View.GONE);
                voiceFrame.setVisibility(View.VISIBLE);
                break;
        }
    }


}
